package com.one.common.config;


import com.google.gson.Gson;
import com.one.common.common.system.model.param.UploadGpsParam;
import com.one.common.common.system.model.response.UserInfoResponse;
import com.one.common.location_service.track.TrackBean;
import com.one.common.utils.Logger;
import com.one.common.utils.StringUtils;

/**
 * Author：zhulunjun
 * Time：2017/9/16
 * description： 内存数据
 */

public class CMemoryData {
    /**
     * 应用版本
     */
    private static String appSource;

    private static boolean roleCarLeader;//当前车主是：车队长角色
    private static boolean roleCarLeaderIn;//内部车队长角色
    private static boolean roleCarLeaderOut;//外部车队长角色
    private static boolean roleCarLeaderAuthSF;//车队长身份认证
    private static boolean isLogin;


    /**
     * 用户信息
     */
    private static UserInfoResponse userInfo;
//    /**
//     * 用户状态信息
//     */
//    private static UserStateResponse userState;
    /**
     * 用户手机号
     */
    private static String userMobile;

    /**
     * 请求用的key
     */
    private static String key;
    private static String keyIvCode;
    private static long getKeyTime;

    /**
     * sessionid
     */
    private static String sessionId;

    /**
     * 位置信息缓存，3分钟一次，2分钟如果要用，调用缓存
     */
    private static UploadGpsParam locationInfo;

    /**
     * 猎鹰轨迹信息
     */
    private static TrackBean trackBean;

    public static boolean isIsLogin() {
        return isLogin;
    }

    public static void setIsLogin(boolean isLogin) {
        CMemoryData.isLogin = isLogin;
    }

    public static TrackBean getTrackBean() {
        if (trackBean == null) {
            String info = CPersisData.getUserInfo();
            if (StringUtils.isNotBlank(info)) {
                trackBean = new Gson().fromJson(info, TrackBean.class);
            }
        }
        if (trackBean == null) {
            trackBean = new TrackBean();
        } else {
            Logger.d("轨迹数据 " + userInfo.toString());
        }
        return trackBean;
    }


    public static UserInfoResponse getUserInfo() {
        if (userInfo == null) {
            String info = CPersisData.getUserInfo();
            if (StringUtils.isNotBlank(info)) {
                userInfo = new Gson().fromJson(info, UserInfoResponse.class);
            }
        }
        if (userInfo == null) {
            userInfo = new UserInfoResponse();
        } else {
            Logger.d("用户信息 " + userInfo.toString());
        }
        return userInfo;
    }
//
//    public static UserStateResponse getUserState() {
//        if (userState == null) {
//            String state = CPersisData.getUserState();
//            if (StringUtils.isNotBlank(state)) {
//                userState = new Gson().fromJson(state, UserStateResponse.class);
//            }
//        }
//        if (userState == null) {
//            userState = new UserStateResponse();
//        } else {
//            Logger.d("用户状态 " + userState.toString());
//        }
//        return userState;
//    }

    public static void setTrackBean(TrackBean trackBean) {
        if (trackBean != null) {
            String infoJson = new Gson().toJson(trackBean);
            if (StringUtils.isNotBlank(infoJson)) {
                CPersisData.setTrackInfo(infoJson);
            }
        }
        CMemoryData.trackBean = trackBean;
    }
//
    public static void setUserInfo(UserInfoResponse userInfo) {
        if (userInfo != null) {
            String infoJson = new Gson().toJson(userInfo);
            if (StringUtils.isNotBlank(infoJson)) {
                CPersisData.setUserInfo(infoJson);
            }
        }
        CMemoryData.userInfo = userInfo;
    }
//
//    public static void setUserState(UserStateResponse state) {
//        if (state != null) {
//            String infoJson = new Gson().toJson(state);
//            if (StringUtils.isNotBlank(infoJson)) {
//                CPersisData.setUserState(infoJson);
//            }
//        }
//        CMemoryData.userState = state;
//    }

    public static void setUserMobile(String userMobile) {
        CPersisData.setMobile(userMobile);
        CMemoryData.userMobile = userMobile;
    }

    public static String getUserMobile() {
        if (StringUtils.isBlank(userMobile)) {
            userMobile = CPersisData.getMobile();

        }
        return userMobile;
    }

    public static String getKey() {
        return key;
    }

    public static void setKey(String key) {
        CMemoryData.key = key;
    }

    public static String getKeyIvCode() {
        return keyIvCode;
    }

    public static void setKeyIvCode(String keyIvCode) {
        CMemoryData.keyIvCode = keyIvCode;
    }

    public static long getGetKeyTime() {
        return getKeyTime;
    }

    public static void setGetKeyTime(long getKeyTime) {
        CMemoryData.getKeyTime = getKeyTime;
    }

    public static String getSessionId() {
        if (StringUtils.isBlank(sessionId)) {
            sessionId = CPersisData.getSessionId();
        }
        Logger.d("session_id " + sessionId);
        return sessionId;
    }

    public static void setSessionId(String sessionId) {
        CMemoryData.sessionId = sessionId;
        CPersisData.setSessionId(sessionId);
    }

    public static void setAppSource(String appSource) {
        CMemoryData.appSource = appSource;
        CPersisData.setAppSource(appSource);
    }

    public static String getAppSorce() {
        if (StringUtils.isBlank(appSource)) {
            appSource = CPersisData.getAppSource();
        }
        return appSource;
    }

    public static boolean isRoleCarLeader() {
        return roleCarLeader;
    }

    public static void setRoleCarLeader(boolean roleCarLeader) {
        CMemoryData.roleCarLeader = roleCarLeader;
    }

    public static boolean isRoleCarLeaderIn() {
        return roleCarLeaderIn;
    }

    public static void setRoleCarLeaderIn(boolean roleCarLeaderIn) {
        CMemoryData.roleCarLeaderIn = roleCarLeaderIn;
    }

    public static boolean isRoleCarLeaderOut() {
        return roleCarLeaderOut;
    }

    public static void setRoleCarLeaderOut(boolean roleCarLeaderOut) {
        CMemoryData.roleCarLeaderOut = roleCarLeaderOut;
    }

    public static boolean isRoleCarLeaderAuthSF() {
        return roleCarLeaderAuthSF;
    }

    public static void setRoleCarLeaderAuthSF(boolean roleCarLeaderAuthSF) {
        CMemoryData.roleCarLeaderAuthSF = roleCarLeaderAuthSF;
    }

    public static void setLocationInfo(UploadGpsParam gpsParam) {
        if (gpsParam != null) {
            String infoJson = new Gson().toJson(gpsParam);
            if (StringUtils.isNotBlank(infoJson)) {
                CPersisData.setLocationInfo(infoJson);
            }
        }
        CMemoryData.locationInfo = gpsParam;
    }

    public static UploadGpsParam getLocationInfo() {
        if (locationInfo == null) {
            String info = CPersisData.getLocationInfo();
            if (StringUtils.isNotBlank(info)) {
                locationInfo = new Gson().fromJson(info, UploadGpsParam.class);
            }
        }
        if (locationInfo == null) {
            locationInfo = new UploadGpsParam();
        } else {
            Logger.d("位置 " + locationInfo.toString());
        }
        return locationInfo;
    }


    public static boolean isCar() {
        return getAppSorce().equals(MyContact.CAR);
    }

    public static boolean isGoods() {
        return getAppSorce().equals(MyContact.GOODS);
    }

    public static boolean isDriver() {
        return getAppSorce().equals(MyContact.DRIVER);
    }


    /**
     * 版本更新APK下载ID
     */
    private static long apkDownloadId;

    public static long getApkDownloadId() {
        return apkDownloadId;
    }

    public static void setApkDownloadId(long apkDownloadId) {
        CMemoryData.apkDownloadId = apkDownloadId;
    }

    public static String getUpdateId() {
        String id = "";
        if (CMemoryData.getAppSorce().equals(MyContact.DRIVER)) {
            id = MyContact.DRIVER_FIR_ID;
        } else if (CMemoryData.getAppSorce().equals(MyContact.CAR)) {
            id = MyContact.CAR_FIR_ID;
        } else if (CMemoryData.getAppSorce().equals(MyContact.GOODS)) {
            id = MyContact.GOODS_FIR_ID;
        }
        return id;

    }
}
